package p270jp.pxv.android.service;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.support.p024v4.media.C0258b;
import android.support.p024v4.media.C0261e;
import android.webkit.MimeTypeMap;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;
import okhttp3.internal.p400ws.WebSocketProtocol;
import p014ae.C0052b6;
import p064c6.Executors;
import p090d0.C2518n;
import p097d7.RunnableC2633l;
import p134ei.AnalyticsAction;
import p134ei.PixivAnalytics;
import p248j2.JobIntentService;
import p270jp.pxv.android.R;
import p270jp.pxv.android.legacy.model.PixivIllust;
import p270jp.pxv.android.legacy.model.PixivNovel;
import p307l9.EdgeTreatment;
import p350mp.Timber;
import p369nh.SaveIllustService;
import p638y5.RequestFutureTarget;
import p677zi.PixivImageLoader;

/* renamed from: jp.pxv.android.service.ImageDownloadService */
/* loaded from: classes2.dex */
public class ImageDownloadService extends JobIntentService {

    /* renamed from: h */
    public static final /* synthetic */ int f19300h;

    /* renamed from: jp.pxv.android.service.ImageDownloadService$a */
    /* loaded from: classes2.dex */
    public enum EnumC4311a {
        REGULAR,
        ORIGINAL
    }

    /* renamed from: e */
    public static void m4562e(Context context) {
        new Handler(Looper.getMainLooper()).post(new RunnableC2633l(context, context.getString(R.string.illust_save_fail), 3));
    }

    /* renamed from: f */
    public static void m4561f(Context context, String str, String str2, EnumC4311a aVar) {
        EdgeTreatment.m3909i(str);
        EdgeTreatment.m3909i(str2);
        Intent intent = new Intent(context, ImageDownloadService.class);
        intent.putExtra("IMAGE_URL", str);
        intent.putExtra("FILE_NAME", str2);
        intent.putExtra("IMAGE_TYPE", aVar);
        ComponentName componentName = new ComponentName(context, ImageDownloadService.class);
        synchronized (JobIntentService.f17834f) {
            JobIntentService.AbstractC3912h b = JobIntentService.m4999b(context, componentName, true, WebSocketProtocol.CLOSE_CLIENT_GOING_AWAY);
            b.m4995b(WebSocketProtocol.CLOSE_CLIENT_GOING_AWAY);
            b.mo4996a(intent);
        }
    }

    /* renamed from: g */
    public static void m4560g(Context context, PixivIllust pixivIllust, int i, EnumC4311a aVar) {
        String str;
        EdgeTreatment.m3909i(pixivIllust);
        int ordinal = aVar.ordinal();
        if (ordinal != 0) {
            if (ordinal != 1) {
                str = null;
            } else if (i == 0 && pixivIllust.pageCount == 1) {
                str = pixivIllust.metaSinglePage.getOriginalImageUrl();
            } else {
                str = pixivIllust.metaPages.get(i).getImageUrls().getOriginal();
            }
        } else if (i == 0) {
            str = pixivIllust.imageUrls.getLarge();
        } else {
            str = pixivIllust.metaPages.get(i).getImageUrls().getLarge();
        }
        String format = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
        String fileExtensionFromUrl = MimeTypeMap.getFileExtensionFromUrl(str);
        StringBuilder c = C0261e.m12042c("illust_");
        c.append(pixivIllust.f19079id);
        c.append("_");
        c.append(format);
        m4561f(context, str, C0258b.m12050a(c, ".", fileExtensionFromUrl), aVar);
    }

    /* renamed from: h */
    public static void m4559h(Context context, PixivNovel pixivNovel) {
        EdgeTreatment.m3909i(pixivNovel);
        String large = pixivNovel.imageUrls.getLarge();
        String format = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
        String fileExtensionFromUrl = MimeTypeMap.getFileExtensionFromUrl(large);
        StringBuilder c = C0261e.m12042c("novel_");
        c.append(pixivNovel.f19079id);
        c.append("_");
        c.append(format);
        m4561f(context, large, C0258b.m12050a(c, ".", fileExtensionFromUrl), EnumC4311a.REGULAR);
    }

    @Override // p248j2.JobIntentService
    @SuppressLint({"MissingPermission", "CheckResult"})
    /* renamed from: c */
    public void mo4563c(Intent intent) {
        String stringExtra = intent.getStringExtra("IMAGE_URL");
        String stringExtra2 = intent.getStringExtra("FILE_NAME");
        PixivAnalytics eVar = (PixivAnalytics) C2518n.m7641d(PixivAnalytics.class);
        int ordinal = ((EnumC4311a) intent.getSerializableExtra("IMAGE_TYPE")).ordinal();
        if (ordinal == 0) {
            eVar.m6528b(14, AnalyticsAction.DOWNLOAD_IMAGE_REGULAR);
        } else if (ordinal == 1) {
            eVar.m6528b(14, AnalyticsAction.DOWNLOAD_IMAGE_ORIGINAL);
        }
        try {
            RequestBuilder<File> O = Glide.m9615e(this).mo1134m().mo1141O(((PixivImageLoader) C2518n.m7641d(PixivImageLoader.class)).m65a(stringExtra));
            Objects.requireNonNull(O);
            RequestFutureTarget fVar = new RequestFutureTarget(Integer.MIN_VALUE, Integer.MIN_VALUE);
            O.m9610K(fVar, fVar, O, Executors.f5347b);
            try {
                ((SaveIllustService) C2518n.m7641d(SaveIllustService.class)).m3363a((File) fVar.get(), stringExtra2).m973c();
                Context applicationContext = getApplicationContext();
                new Handler(Looper.getMainLooper()).post(new RunnableC2633l(applicationContext, applicationContext.getString(R.string.illust_save_success), 3));
            } catch (Throwable th) {
                Timber.f23100a.mo3514p(new Exception(C0052b6.m12149a("保存できませんでした fileName: ", stringExtra2), th));
                m4562e(getApplicationContext());
            }
        } catch (Exception unused) {
            m4562e(getApplicationContext());
        }
    }
}
